<template>
  <div class="spanControl-list" style="width: 100%;margin-left: 0!important;">
    <el-card class="box-card card-head" style="margin: 0px; width: 100%; min-height: 99%">
      <div slot="header" class="clearfix">
        <span>商家管理</span>
      </div>
      <div class="alert-container clearfix">
        <el-alert v-for="alert in alerts.successes" :key="alert.title"
                  v-bind:title="alert.title"
                  type="success"
                  show-icon>
        </el-alert>
        <el-alert v-for="alert in alerts.infos" :key="alert.title"
                  v-bind:title="alert.title"
                  type="info"
                  show-icon>
        </el-alert>
        <el-alert v-for="alert in alerts.warnings" :key="alert.title"
                  v-bind:title="alert.title"
                  type="warning"
                  show-icon>
        </el-alert>
        <el-alert v-for="alert in alerts.errors" :key="alert.title"
                  v-bind:title="alert.title"
                  type="error"
                  show-icon>
        </el-alert>
      </div>
      <div class="text item">
        <el-form ref="businessForm" :model="business" label-width="150px" :rules="rules">
          <!--<el-col :span="11">-->
          <!--<el-form-item label="名称" prop="name">-->
          <!--<file-tip content="名称"></file-tip>-->
          <!--<el-input type="input" v-model="business.name"-->
          <!--placeholder="名称" clearable autosize-->
          <!--resize ="both" tabindex=1-->
          <!--maxlength=255-->
          <!--&gt;</el-input>-->
          <!--</el-form-item>-->
          <!--</el-col>-->
          <!--<el-col :span="11">-->
          <!--<el-form-item label="商家编号" prop="businessCode">-->
          <!--<file-tip content="商家编号"></file-tip>-->
          <!--<el-input type="input" v-model="business.businessCode"-->
          <!--placeholder="商家编号" clearable autosize-->
          <!--resize ="both" tabindex=3-->
          <!--maxlength=20-->
          <!--&gt;</el-input>-->
          <!--</el-form-item>-->
          <!--</el-col>-->
          <el-col :span="11">
            <el-form-item label="商家名称" prop="businessName">
              <file-tip content="商家名称"></file-tip>
              <el-input type="input" v-model="business.businessName"
                        placeholder="商家名称" clearable autosize
                        resize="both" tabindex=4
                        maxlength=30
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="法人" prop="legalPerosn">
              <file-tip content="法人"></file-tip>
              <el-input type="input" v-model="business.legalPerosn"
                        placeholder="法人" clearable autosize
                        resize="both" tabindex=5
                        maxlength=10
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系人" prop="contact">
              <file-tip content="联系人"></file-tip>
              <el-input type="input" v-model="business.contact"
                        placeholder="联系人" clearable autosize
                        resize="both" tabindex=6
                        maxlength=10
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系人电话" prop="contactPhone">
              <file-tip content="联系人电话"></file-tip>
              <el-input type="input" v-model="business.contactPhone"
                        placeholder="联系人电话" clearable autosize
                        resize="both" tabindex=7
                        maxlength=11
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="省市区">
              <el-cascader
                expand-trigger="hover"
                :options="businessAddresses"
                v-model="selectedBusinessAddresses"
                @change="handleChange">
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="商铺详细地址" prop="businessAddress">
              <file-tip content="商铺详细地址"></file-tip>
              <el-input type="input" v-model="business.businessAddress"
                        placeholder="商铺详细地址" clearable autosize
                        resize="both" tabindex=11
                        maxlength=100
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="身份证号码" prop="idNumber">
              <file-tip content="身份证号码"></file-tip>
              <el-input type="input" v-model="business.idNumber"
                        placeholder="身份证号码" clearable autosize
                        resize="both" tabindex=12
                        maxlength=18
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="营业执照号码" prop="licenseNumber">
              <file-tip content="营业执照号码"></file-tip>
              <el-input type="input" v-model="business.licenseNumber"
                        placeholder="营业执照号码" clearable autosize
                        resize="both" tabindex=13
                        maxlength=60
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="主营产品" prop="mainProducts">
              <file-tip content="主营产品"></file-tip>
              <el-input type="input" v-model="business.mainProducts"
                        placeholder="主营产品" clearable autosize
                        resize="both" tabindex=14
                        maxlength=64
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="营业开始时间" prop="beginHours">
              <el-time-select clearable
                              v-model="business.beginHours"
                              align="right"
                              placeholder="营业时间"
                              :picker-options="{
                                                   start: '08:30',
                                                   step: '00:15',
                                                   end: '18:30'
                                                }">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="营业结束时间">
              <el-time-select clearable
                              v-model="business.endHours"
                              align="right"
                              placeholder="营业结束时间"
                              :picker-options="{
                                                   start: '08:30',
                                                   step: '00:15',
                                                   end: '18:30'
                                                }">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="qq号码" prop="qqNumber">
              <file-tip content="qq号码"></file-tip>
              <el-input type="input" v-model="business.qqNumber"
                        placeholder="qq号码" clearable autosize
                        resize="both" tabindex=17
                        maxlength=12
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="邮箱" prop="email">
              <file-tip content="邮箱"></file-tip>
              <el-input type="input" v-model="business.email"
                        placeholder="邮箱" clearable autosize
                        resize="both" tabindex=18
                        maxlength=30
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="注册时间" prop="registrationTime">
              <el-date-picker clearable
                              v-model="business.registrationTime"
                              align="right"
                              type="date"
                              placeholder="注册时间"
                              :picker-options="pickerOptionsRegistrationTime"
                              format="yyyy-MM-dd"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="商家状态" prop="businessStatus" v-if="edit">
              <el-autocomplete
                class="inline-input"
                value-key="name"
                v-model="business.businessStatus"
                :fetch-suggestions="searchBusinessStatus "
                placeholder="商家状态"
                clearable autosize
                resize="both" tabindex="20"
              ></el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="商家描述" prop="description">
              <file-tip content="商家描述"></file-tip>
              <el-input type="input" v-model="business.description"
                        placeholder="商家描述" clearable autosize
                        resize="both" tabindex=21
                        maxlength=500
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22" v-if="edit">
            <el-form-item label="审核资料上传" v-if="!edit">
              <file-upload
                :files="files"
                :fileArray="fileArray"
                :edit="edit"
                :nextUrl="uploadNextUrl">
              </file-upload>
            </el-form-item>

          </el-col>
          <el-col :span="20" v-if="edit">
            <el-form-item label="审核资料">
              <div v-for="file in fileList">
                <img height="150px" width="200" style="text-align: center;margin-left: 0px"
                     :src="headerFile+file.attachmentAddr" alt="">
                <el-row>
                   <span style="color: blue;cursor: pointer"
                         @click="down(file.attachmentAddr)">下载</span> <span
                  style="color: blue;cursor: pointer" @click="view(file.attachmentAddr)">预览</span><span
                  style="color:red;cursor: pointer" @click="deleteFile(file.eid)">删除</span>
                </el-row>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item>
              <el-button type="primary" @click="submitBusiness()" :loading="isSubmiting"
                         v-permission:simple_businessRegistration_Business_Edit>提交
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
        <div>

        </div>
      </div>
      <div class="text item clearfix">
        <el-alert v-for="remark in alerts.remarks" :key="remark.title"
                  v-bind:title="remark.title"
                  type="info"
                  v-bind:description="remark.content">
        </el-alert>
      </div>
    </el-card>
  </div>

</template>

<script>


  import {ProvincesService} from '../../simple/businessRegistration/ProvincesService'
  import {CityService} from '../../simple/businessRegistration/CityService'
  import {AreaService} from '../../simple/businessRegistration/AreaService'
  import {BusinessService} from './BusinessService'
  import {AttachmentService} from "../attachment/AttachmentService";
  import {d4utils} from '../../../tools/d4utils'
  import {Msg} from '../../../tools/message'
  import fileTip from '@/components/tip/fileTip'
  import fileUpload from '../../simple/uploadTest/index'

  export default {
    components: {
      fileTip,
      fileUpload
    },
    data() {
      var validateIntRange = d4utils.validateFloatRange;
      var validateFloatRange = d4utils.validateFloatRange;
      var validateString = d4utils.validateString;

      return {
        edit: null,
        fileList: null,
        nextUrl: null,
        uploadNextUrl: '/simple/businessRegistration/business',
        attachments: [],
        fileArray: [],
        headerFile: process.env.BASE_FILE_VIEW,
        files: [],
        formName: 'simple_business',
        rules: {
          name: [
            {required: false, message: '请输入名称', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          businessCode: [
            {required: true, message: '请输入商家编号', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          businessName: [
            {required: true, message: '请输入商家名称', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          legalPerosn: [
            {required: true, message: '请输入法人', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          contact: [
            {required: true, message: '请输入联系人', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          contactPhone: [
            {required: true, message: '请输入联系人电话', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          provincesId: [
            {required: true, message: '请输入省份', trigger: 'blur'},
          ],
          provincesName: [
            {required: false, message: '请输入省份', trigger: 'blur'},
          ],
          cityId: [
            {required: true, message: '请输入市', trigger: 'blur'},
          ],
          cityName: [
            {required: false, message: '请输入市', trigger: 'blur'},
          ],
          areaName: [
            {required: false, message: '请输入区', trigger: 'blur'},
          ],
          areaId: [
            {required: true, message: '请输入区', trigger: 'blur'},
          ],
          businessAddress: [
            {required: true, message: '请输入商铺详细地址', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          idNumber: [
            {required: true, message: '请输入身份证号码', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          licenseNumber: [
            {required: true, message: '请输入营业执照号码', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          mainProducts: [
            {required: true, message: '请输入主营产品', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          beginHours: [
            {required: true, message: '请输入营业开始时间', trigger: 'change'},
          ],
          endHours: [
            {required: true, message: '请输入营业结束时间', trigger: 'change'},
          ],
          qqNumber: [
            {required: true, message: '请输入qq号码', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          registrationTime: [
            {required: true, message: '请输入注册时间', trigger: 'change'},
          ],
          businessStatus: [
            {required: false, message: '请输入商家状态', trigger: 'blur'},
          ],
          description: [
            {required: false, message: '请输入商家描述', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
          score: [
            {required: false, message: '请输入评分', trigger: 'blur'},
            {validator: validateFloatRange(-9223372036854775808, 9223372036854775807), trigger: 'blur'},
          ],
          remark: [
            {required: false, message: '请输入备注', trigger: 'blur'},
            {validator: validateString(0, 1000, /^.*$/, "输入的数据不正确，请检查"), trigger: 'blur'},
          ],
        },
        testTime: new Date(),
        isSubmiting: false,
        business: {
          endHours: null,
          beginHours: null,
          businessName: null,
        },
        businessId: null,
        pickerOptionsBeginHours: {
          disabledDate(time) {
            //TODO: 请在此判断可以输入的日期范围,
            //return time.getTime() > Date.now();
            return false;
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        pickerOptionsEndHours: {
          disabledDate(time) {
            //TODO: 请在此判断可以输入的日期范围,
            //return time.getTime() > Date.now();
            return false;
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        pickerOptionsRegistrationTime: {
          disabledDate(time) {
            //TODO: 请在此判断可以输入的日期范围,
            //return time.getTime() > Date.now();
            return false;
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        pickerOptionsCreateDatetime: {
          disabledDate(time) {
            //TODO: 请在此判断可以输入的日期范围,
            //return time.getTime() > Date.now();
            return false;
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        pickerOptionsUpdateDatetime: {
          disabledDate(time) {
            //TODO: 请在此判断可以输入的日期范围,
            //return time.getTime() > Date.now();
            return false;
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        alerts: {
          remarks: [{title: "功能说明", content: "TODO: 请在这里加上功能说明"},],
          successes: [], //TODO:[{title:'消息内容'},]
          infos: [],
          warnings: [],
          errors: []
        },
        businessAddresses: [],
        selectedBusinessAddresses: [],
      }
    },
    watch: {
      business: {
        handler(newValue, oldValue) {
          // console.log(typeof(business.beginHours)+typeof(business.endHours)+'change')
        },
        immediate: true,
        deep: true
      }
    },
    methods: {
      view(path) {
        window.open(process.env.BASE_FILE_VIEW + "" + path + '&isOnline=true')
      },
      down(path) {
        window.open(process.env.BASE_FILE_VIEW + "" + path)
      },
      deleteFile(id) {
        AttachmentService.deleteAttachment(id).then((res) => {
          Msg.success("删除成功")
          this.findBusinessForEdit(this.businessId);
        })
          .catch((err) => {
            Msg.error("删除失败")
          })
      },
      handleChange(value) {
        for (let i = 0; i < 3; i++) {
          if (i === 0) {
            this.business.provincesId = value[i];
            this.provincesProvincess.forEach((provinces) => {
              if (value[i] === provinces.eid) {
                this.business.provincesName = provinces.name;
              }
            })
          }
          if (i === 1) {
            this.business.cityId = value[i];
            this.cityCitys.forEach((city) => {
              if (value[i] === city.eid) {
                this.business.cityName = city.name;
              }
            })
          }
          if (i === 2) {
            this.business.areaId = value[i];
            this.areaAreas.forEach((area) => {
              if (value[i] === area.eid) {
                this.business.areaName = area.name;
              }
            })
          }
        }
      },
      addAlert(message, title, type) {
        //type 可选的值为:remarks,successes,infos,warnings,errors
        type = type ? type : "errors";
        type = this.alerts[type] ? type : "errors";
        title = title ? title : message;
        this.alerts[type].unshift({title: title, content: message});
      },
      removeAlert(title, type) {
        //type 可选的值为:remarks,successes,infos,warnings,errors
        type = type ? type : "errors";
        type = this.alerts[type] ? type : "errors";
        for (var i = this.alerts[type].length - 1; i >= 0; i--) {
          delete this.alerts[type][i]
        }
      },
      submitBusiness() {
        var refs = this.$refs;
        refs['businessForm'].validate(valid => {
          if (valid) {
            if (this.business.eid)//编辑商家
            {
              this.updateBusiness();
            } else//保存商家
            {
              this.saveBusiness();
            }
          } else {
            return false
          }
        })
      },
      saveBusiness()//保存商家
      {
        this.isSubmiting = true;
        this.buttonRequestProgressStart("正在保存,请稍后...");
        this.business.name = this.business.businessName;
        this.business.businessStatus = "待审核"
        BusinessService.saveBusiness(this.business).then((resp) => {
          this.buttonRequestProgressClose();
          this.isSubmiting = false;
          var router = this.$router;
          router.push({path: this.nextUrl + this.business.businessName, query: {}})
        }).catch((error) => {
          this.buttonRequestProgressClose();
          //error的data属性是后台传入的数据,data的extendedData存有后台传入的数据
          this.isSubmiting = false;

          this.addAlert({title: error.data.message})
          this.$message({
            type: 'error',
            message: '保存出错,原因是：' + error.data.message
          })
        })
      },
      updateBusiness()//编辑商家
      {
        this.isSubmiting = true;
        this.buttonRequestProgressStart("正在更新,请稍后...");

        BusinessService.updateBusiness(this.business).then((resp) => {
          this.buttonRequestProgressClose();
          this.isSubmiting = false;
          var router = this.$router;
          router.push({path: this.nextUrl})
        }).catch((error) => {
          this.buttonRequestProgressClose();
          //error的data属性是后台传入的数据,data的extendedData存有后台传入的数据
          this.isSubmiting = false;
          this.addAlert({title: error.data.message})
          this.$message({
            type: 'error',
            message: '保存出错,原因是：' + error.data.message
          })

        })
      },
      findBusinessForEdit(businessId)//查找商家
      {
        BusinessService.findBusinessForEdit(businessId).then((resp) => {
          this.prepareForEdit(resp.data);

        }).catch((error) => {
          this.$message({
            type: 'error',
            message: '查询商家出错'
          })
        })
        BusinessService.findAllUploadedFilesByIdAndName(this.businessId, this.formName)
          .then((res) => {
            this.attachments = res.data;
            this.fileList = this.attachments;
            this.attachments.forEach((attachment) => {
              BusinessService.downloadAttachments(false, attachment.attachmentAddr)
                .then((res) => {
                  console.log("附件下载成功")
                  let File = {};
                  File = attachment;
                  this.files.push(attachment);
                  this.fileArray = this.attachments;
                })
                .catch((err) => {
                  console.log("附件下载失败")
                })
            })
            console.log("附件读取成功：" + JSON.stringify(this.files))

          })
          .catch((err) => {
            console.log("附件读取失败")
          })


      },
      createBusiness()//创建新的商家
      {
        BusinessService.createBusiness().then((resp) => {
          this.prepareForEdit(resp.data);
        }).catch((error) => {
          this.$message({
            type: 'error',
            message: '创建新的商家出错'
          })
        })
      },
      prepareForEdit(businessEditDto) {
        this.business = businessEditDto.business;
        this.businessStatusCodeTables = businessEditDto.businessStatusCodeTables;
        this.provincesProvincess = businessEditDto.provincesProvincess
        this.cityCitys = businessEditDto.cityCitys
        this.areaAreas = businessEditDto.areaAreas
        this.provincesProvincess.forEach((provinces) => {
          let children = []
          let provincesItem = {}
          provincesItem.value = provinces.eid;
          provincesItem.label = provinces.name;
          this.cityCitys.forEach((city) => {
            if (city.provincesId === provinces.eid) {
              let cityItem = {}
              cityItem.value = city.eid;
              cityItem.label = city.name;
              let areaList = [];
              this.areaAreas.forEach((area) => {
                if (area.cityId === city.eid) {
                  let areaItem = {}
                  areaItem.value = area.eid;
                  areaItem.label = area.name;
                  areaList.push(areaItem);
                }
              })
              cityItem.children = areaList;
              children.push(cityItem)
            }
          })
          provincesItem.children = children;
          this.businessAddresses.push(provincesItem)
        })
        if (this.business.name != null) {
          this.selectedBusinessAddresses[0] = this.business.provincesId;
          this.selectedBusinessAddresses[1] = this.business.cityId;
          this.selectedBusinessAddresses[2] = this.business.areaId;
        }
      },
      searchBusinessStatus(queryString, cb) {
        var businessStatusCodeTables = this.businessStatusCodeTables;
        var results = queryString ? businessStatusCodeTables.filter(this.createFilterBusinessStatus(queryString)) : businessStatusCodeTables;
        cb(results);
      },
      createFilterBusinessStatus(queryString) {
        return (BusinessStatus) => {
          return (BusinessStatus.name.toLowerCase().indexOf(queryString.toLowerCase()) >= 0);
        };
      },


      searchProvincesName(queryString, cb) {
        var provincesProvincess = this.provincesProvincess;

        //如果不为空，说明在加载页面的时候已经加载了所有的外键数据，否则表示没加载，则根据输入的信息从后台加载
        if (provincesProvincess) {
          var results = queryString ? provincesProvincess.filter(this.createFilterProvincesName(queryString)) : provincesProvincess;
          cb(results);
        } else {
          var results = d4utils.getObjectCached("findProvincessWithIdNameByName_" + queryString);
          if (results) {
            cb(results);
          } else {
            var cb1 = cb;
            ProvincesService.findProvincessWithIdNameByName(queryString).then((resp) => {
              if (resp.data) {
                var results = resp.data;
                d4utils.cacheObject("findProvincessWithIdNameByName_" + queryString);
                cb1(results)
              } else {
                this.$message({
                  type: 'error',
                  message: '没有查询到引用数据'
                });
              }
            }).catch((error) => {
              this.$message({
                type: 'error',
                message: '查询引用数据出错'
              })
            });
          }
        }
      },
      createFilterProvincesName(queryString) {
        return (provinces) => {
          return (provinces.name.toLowerCase().indexOf(queryString.toLowerCase()) >= 0);
        };
      },
      handleSelectProvincesName(item) {
        this.business.provincesId = item.eid;
      },

      searchCityName(queryString, cb) {
        var cityCitys = this.cityCitys;

        //如果不为空，说明在加载页面的时候已经加载了所有的外键数据，否则表示没加载，则根据输入的信息从后台加载
        if (cityCitys) {
          var results = queryString ? cityCitys.filter(this.createFilterCityName(queryString)) : cityCitys;
          cb(results);
        } else {
          var results = d4utils.getObjectCached("findCitysWithIdNameByName_" + queryString);
          if (results) {
            cb(results);
          } else {
            var cb1 = cb;
            CityService.findCitysWithIdNameByName(queryString).then((resp) => {
              if (resp.data) {
                var results = resp.data;
                d4utils.cacheObject("findCitysWithIdNameByName_" + queryString);
                cb1(results)
              } else {
                this.$message({
                  type: 'error',
                  message: '没有查询到引用数据'
                });
              }
            }).catch((error) => {
              this.$message({
                type: 'error',
                message: '查询引用数据出错'
              })
            });
          }
        }
      },
      createFilterCityName(queryString) {
        return (city) => {
          return (city.name.toLowerCase().indexOf(queryString.toLowerCase()) >= 0);
        };
      },
      handleSelectCityName(item) {
        this.business.cityId = item.eid;
      },

      searchAreaName(queryString, cb) {
        var areaAreas = this.areaAreas;

        //如果不为空，说明在加载页面的时候已经加载了所有的外键数据，否则表示没加载，则根据输入的信息从后台加载
        if (areaAreas) {
          var results = queryString ? areaAreas.filter(this.createFilterAreaName(queryString)) : areaAreas;
          cb(results);
        } else {
          var results = d4utils.getObjectCached("findAreasWithIdNameByName_" + queryString);
          if (results) {
            cb(results);
          } else {
            var cb1 = cb;
            AreaService.findAreasWithIdNameByName(queryString).then((resp) => {
              if (resp.data) {
                var results = resp.data;
                d4utils.cacheObject("findAreasWithIdNameByName_" + queryString);
                cb1(results)
              } else {
                this.$message({
                  type: 'error',
                  message: '没有查询到引用数据'
                });
              }
            }).catch((error) => {
              this.$message({
                type: 'error',
                message: '查询引用数据出错'
              })
            });
          }
        }
      },
      createFilterAreaName(queryString) {
        return (area) => {
          return (area.name.toLowerCase().indexOf(queryString.toLowerCase()) >= 0);
        };
      },
      handleSelectAreaName(item) {
        this.business.areaId = item.eid;
      },
      findBusinessAttachment() {

      }
    },
    created() {
      this.businessId = this.$route.params.businessId;
      if (this.businessId)//编辑
      {
        this.edit = true
        this.nextUrl = '/simple/businessRegistration/business'
        this.findBusinessForEdit(this.businessId);

      } else//新增
      {
        this.edit = false
        this.nextUrl = '/simple/uploadTest/'
        this.createBusiness();
      }
    },
  }
</script>
<style scoped lang="scss">

</style>
